home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / x / xwaisq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-04  |  14.5 KB  |  506 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    This is part of the X user-interface for the WAIS software.  Do with it
  6.    as you please.
  7.  
  8.    jonathan@Think.COM
  9. */
  10.  
  11. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  12.  
  13. /*
  14.  * $Log: xwaisq.c,v $
  15.  * Revision 1.2  1994/08/05  07:29:04  pfeifer
  16.  * Release beta 04
  17.  *
  18.  * Revision 1.1  1993/02/16  15:10:18  freewais
  19.  * Initial revision
  20.  *
  21.  * Revision 1.22  92/06/15  13:51:38  jonathan
  22.  * Changed call to fuzzbuttons.
  23.  * 
  24.  * Revision 1.21  92/06/03  17:09:00  jonathan
  25.  * Changed Scandirs to leave listwidgets where they are.  Less confusing.
  26.  * 
  27.  * Revision 1.20  92/04/28  15:34:58  jonathan
  28.  * Cleaned up includes.  Replaced references to directory routines with
  29.  * scandir.
  30.  * 
  31.  * Revision 1.19  92/03/23  16:11:30  jonathan
  32.  * Added stuff to set button sensitivity.
  33.  * 
  34.  * Revision 1.18  92/03/17  15:08:23  jonathan
  35.  * Typed ExitCommand.
  36.  * 
  37.  * Revision 1.17  92/03/17  14:22:32  jonathan
  38.  * Generally cleaned up.
  39.  * 
  40.  * Revision 1.16  92/03/07  19:39:23  jonathan
  41.  * Fixed argument to ScanDirs.
  42.  * 
  43.  * Revision 1.15  92/03/06  15:01:51  jonathan
  44.  * Moved freopen to right before main loop.
  45.  * 
  46.  * Revision 1.14  92/03/06  14:49:19  jonathan
  47.  * New and Improved source loading!  Also rescans source directory if it
  48.  * changes.
  49.  * 
  50.  * Revision 1.13  92/03/01  14:04:06  jonathan
  51.  * Added command_name to main.
  52.  * 
  53.  */
  54.  
  55. #ifndef lint
  56. static char *RCSid = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/x/RCS/xwaisq.c,v 1.2 1994/08/05 07:29:04 pfeifer Exp $";
  57. #endif
  58.  
  59. #define XWAISQ_C
  60. #define MAIN
  61. #include "xwais.h"
  62. #include <sys/stat.h>
  63.  
  64. /* Dummy function added so we can link to wais-ui.c.  swais uses the
  65.  * real version of this routine, which is in swais.c, but it's called
  66.  * in here -- warnock@hypatia.gsfc.nasa.gov, 7/21/93
  67.  */
  68. void
  69. DumpTextToScreen(buffer, size)
  70. char *buffer;
  71. long size;
  72. {
  73. }   
  74.  
  75. #define offset(field) XtOffset(struct _app_resources*, field)
  76. static XtResource resources[] = {
  77.    {"questionDirectory", "QuestionDirectory", XtRString, sizeof(char *),
  78.          offset(questionDirectory), XtRString, "~/wais-questions/"},
  79.    {"userSourceDirectory", "UserSourceDirectory", XtRString, sizeof(char *),
  80.          offset(userSourceDirectory), XtRString, "~/wais-sources/"},
  81.    {"commonSourceDirectory", "CommonSourceDirectory", XtRString, sizeof(char *),
  82.          offset(commonSourceDirectory), XtRString, ""},
  83.    {"documentDirectory", "DocumentDirectory", XtRString, sizeof(char *),
  84.          offset(documentDirectory), XtRString, "~/wais-documents/"},
  85.    {"helpFile", "HelpFile", XtRString, sizeof(char *),
  86.          offset(helpFile), XtRString, "./XwaisHELP"},
  87.    {"removeSeekerCodes", "RemoveSeekerCodes", XtRString, sizeof(char *),
  88.       offset(removeSeekerCodes), XtRString, "On"},
  89.    {"rescanInterval", "RescanInterval", XtRString, sizeof(char *),
  90.       offset(rescanInterval), XtRString, "1"},
  91.    {"seedWords", "SeedWords", XtRString, sizeof(char *),
  92.       offset(seedWords), XtRString, ""},
  93.    {"initialSource", "InitialSource", XtRString, sizeof(char *),
  94.       offset(initialSource), XtRString, ""},
  95.    {"questionName", "QuestionName", XtRString, sizeof(char *),
  96.       offset(questionName), XtRString, ""},
  97.    {"filters", "Filters", XtRString, sizeof(char *),
  98.       offset(filters), XtRString, ""},
  99.    {"maximumResults", "MaximumResults", XtRInt, sizeof(int),
  100.       offset(maximumResults), XtRImmediate, (caddr_t) 40},
  101.    {"doSearch", "DoSearch", XtRBoolean, sizeof(Boolean),
  102.       offset(doSearch), XtRBoolean, False},
  103.    {"defaultsInstalled", "DefaultsInstalled",  XtRBoolean, sizeof(Boolean),
  104.       offset(defaultsInstalled), XtRBoolean, False},
  105.    {"showKeys", "ShowKeys",  XtRBoolean, sizeof(Boolean),
  106.       offset(showKeys), XtRBoolean, False}
  107. };
  108. #undef offset
  109.  
  110. XtActionsRec xwaisActionsTable[] = {
  111.   /* special for return only */
  112.   {(String)"waissearch", (XtActionProc)DoSearch},
  113.   {(String)"save", (XtActionProc)DoSave},
  114.   {(String)"setdir", (XtActionProc)SetDir},
  115.   {(String)"dopsave", (XtActionProc)DoPSave},
  116.   NULL
  117.   };
  118.  
  119. void ExitCommand()
  120. {
  121.   exit(-1);
  122. }
  123.  
  124. PopExit(parent, message)
  125. Widget parent;
  126. char *message;
  127. {
  128.   Widget shell, frame, labelwid, stringlabelwid;
  129.   WidgetClass wclass;
  130.   static String namestring;
  131.   Arg        args[5];
  132.   Position    x, y;
  133.   Dimension    width, height;
  134.   Cardinal    n;
  135.  
  136.   shell = XtCreatePopupShell("exitpopup", applicationShellWidgetClass,
  137.                  parent, NULL, ZERO);
  138.   frame = XtCreateManagedWidget("exitpopupform", formWidgetClass,
  139.                 shell, NULL, ZERO);
  140.  
  141.   labelwid = MakeLabel(frame, "exitlabel", message, NULL, NULL);
  142.  
  143.   MakeCommandButton(frame, "Ok", ExitCommand, labelwid, NULL, NULL);
  144.  
  145.   n = 0;
  146.   XtSetArg(args[n], XtNx, 100); n++;
  147.   XtSetArg(args[n], XtNy, 100); n++;
  148.   XtSetArg(args[n], XtNtitle, "XWAIS Question Error"); n++;
  149.   XtSetValues(shell, args, n);
  150.  
  151.   XtPopup(shell, XtGrabExclusive);
  152. }
  153.  
  154.  
  155. static time_t usersourcetime, commonsourcetime, questiontime;
  156. static int rescanint;
  157.  
  158. void ScanDirs(closure, id)
  159.      Opaque closure;
  160.      XtIntervalId *id;
  161. {
  162.   char **list_data;
  163.   float top, shown;
  164.   int CurrentQuestion, CurrentSource, i;
  165.   struct stat buf;
  166.   boolean rescan;
  167.   char string[STRINGSIZE];
  168.  
  169.   rescan = FALSE;
  170.  
  171.   stat(app_resources.userSourceDirectory, &buf);
  172.  
  173.   if(buf.st_mtime != usersourcetime) {
  174.     usersourcetime = buf.st_mtime;
  175.     rescan = TRUE;
  176.   }
  177.  
  178.   if(app_resources.commonSourceDirectory[0] != 0) {
  179.     stat(app_resources.commonSourceDirectory, &buf);
  180.  
  181.     if(buf.st_mtime != commonsourcetime) {
  182.       commonsourcetime = buf.st_mtime;
  183.       rescan = TRUE;
  184.     }
  185.   }
  186.  
  187.   if (rescan) {
  188.     rescan = FALSE;
  189.  
  190.     CurrentSource = get_selected_source();
  191.     if (CurrentSource != NO_ITEM_SELECTED)
  192.       strcpy(string, Source_items[CurrentSource]);
  193.  
  194.     NumSources = 0;
  195.  
  196.     GetSourceNames(app_resources.userSourceDirectory);
  197.     if(app_resources.commonSourceDirectory[0] != 0)
  198.       GetSourceNames(app_resources.commonSourceDirectory);
  199.  
  200.     RebuildListWidget(sourcewindow, Source_items, LIST_NONE);
  201.  
  202.     if(CurrentSource != NO_ITEM_SELECTED) {
  203.       for(i = 0;
  204.       Source_items[i] != NULL && 
  205.       strcmp(Source_items[i], string) != 0;
  206.       i++);
  207. #ifndef MOTIF
  208.       if(i < NumSources) XawListHighlight(sourcewindow->ListWidget, i);
  209. #endif
  210.     }
  211.   }
  212.  
  213.   rescantimerid = XtAddTimeOut(rescanint, ScanDirs, (Opaque) ScanDirs);
  214. }
  215.  
  216. /* this will take as its argument the name of a question file,
  217.    and will do all the right things with it.
  218. */
  219.  
  220. void
  221. main(argc, argv)
  222.      int argc;
  223.      char *argv[];
  224. {
  225.   struct stat buf;
  226.   long count;
  227.   Widget frame, labelwid, stringlabelwid, button;
  228.   WidgetClass wclass;
  229.   static String
  230.     name = "";
  231.   Arg arglist[10];
  232.   Cardinal num_args;
  233.   static String items[] = {NULL};
  234.   int n;
  235.   float shown;
  236.   Question question;
  237.   char msg[STRINGSIZE];
  238.   char *cdir, *sdir;
  239.  
  240.  
  241.   Type_items = (char**)s_malloc(20*sizeof(char*));
  242.   Type_items[0] = NULL;
  243.  
  244.   if (command_name = (char*)rindex(argv[0], '/'))
  245.     command_name++;
  246.   else
  247.     command_name = argv[0];
  248.  
  249.   
  250.   the_Source = (Source) s_malloc(sizeof(_Source));
  251.  
  252.   NumSources = 0;
  253.  
  254.   allText = NULL;
  255.  
  256.   double_click = FALSE;
  257.  
  258.   top = XtInitialize("xwais", "Xwais", NULL, 0, &argc, argv);
  259.   
  260.   XtAddActions(xwaisActionsTable, XtNumber(xwaisActionsTable));
  261.  
  262.   XtGetApplicationResources(top, &app_resources, resources,
  263.                 XtNumber(resources), NULL, 0);
  264.  
  265.   app_context = XtWidgetToApplicationContext(top);
  266.  
  267.   if (app_resources.defaultsInstalled == False) {
  268.     PopExit(top, "X resources not properly installed");
  269.     XtMainLoop();
  270.   }
  271.  
  272.   if(app_resources.documentDirectory[0] == '~') {
  273.     char *home, *dir, *getenv();
  274.     
  275.     if((home = getenv("HOME")) != NULL) {
  276.       if((dir = s_malloc(strlen(home) +
  277.                strlen(app_resources.documentDirectory) +
  278.                2)) == NULL) {
  279.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  280.     exit(-1);
  281.       }
  282.       strcpy(dir, home);
  283.       strcat(dir, &app_resources.documentDirectory[1]);
  284.       app_resources.documentDirectory=dir;
  285.     }
  286.   }
  287.  
  288.   if(app_resources.userSourceDirectory[0] == '~') {
  289.     char *home, *dir, *getenv();
  290.     
  291.     if((home = getenv("HOME")) != NULL) {
  292.       if((dir = s_malloc(strlen(home) +
  293.                strlen(app_resources.userSourceDirectory) +
  294.                2)) == NULL) {
  295.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  296.     exit(-1);
  297.       }
  298.       strcpy(dir, home);
  299.       strcat(dir, &app_resources.userSourceDirectory[1]);
  300.       app_resources.userSourceDirectory=dir;
  301.     }
  302.   }
  303.   sdir = app_resources.userSourceDirectory;
  304.   cdir = app_resources.commonSourceDirectory;
  305.  
  306.   stat(app_resources.userSourceDirectory, &buf);
  307.   usersourcetime = buf.st_mtime;
  308.   GetSourceNames(app_resources.userSourceDirectory);
  309.  
  310.   if(app_resources.commonSourceDirectory[0] != 0) {
  311.     stat(app_resources.commonSourceDirectory, &buf);
  312.     commonsourcetime = buf.st_mtime;
  313.     GetSourceNames(app_resources.commonSourceDirectory);
  314.   }
  315.  
  316.   if(app_resources.questionDirectory[0] == '~') {
  317.     char *home, *dir, *getenv();
  318.     
  319.     if((home = getenv("HOME")) != NULL) {
  320.       if((dir = s_malloc(strlen(home) +
  321.                strlen(app_resources.questionDirectory) +
  322.                2)) == NULL) {
  323.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  324.     exit(-1);
  325.       }
  326.       strcpy(dir, home);
  327.       strcat(dir, &app_resources.questionDirectory[1]);
  328.       app_resources.questionDirectory=dir;
  329.     }
  330.   }
  331.   /* let's see if it exists, if not, try to create it */
  332.   {
  333.     DIR *dirp;
  334.     char *makedir;
  335.  
  336.     if((dirp = opendir(app_resources.questionDirectory)) == NULL) {
  337.       if((makedir = s_malloc(strlen(app_resources.questionDirectory) + 12))
  338.      == NULL) {
  339.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  340.     exit(-1);
  341.       }
  342.       strcpy(makedir, "/bin/mkdir ");
  343.       strcat(makedir, app_resources.questionDirectory);
  344.       if(makedir[strlen(makedir)-1] == '/') makedir[strlen(makedir)-1] = 0;
  345.       if(system(makedir) != 0)
  346.     fprintf(stderr, "Error creating directory: %s\n",
  347.         app_resources.questionDirectory);
  348.     }
  349.     else
  350.       closedir(dirp);
  351.  
  352.     if((dirp = opendir(app_resources.userSourceDirectory)) == NULL) {
  353.       if((makedir = s_malloc(strlen(app_resources.userSourceDirectory) + 12))
  354.      == NULL) {
  355.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  356.     exit(-1);
  357.       }
  358.       strcpy(makedir, "/bin/mkdir ");
  359.       strcat(makedir, app_resources.userSourceDirectory);
  360.       if(makedir[strlen(makedir)-1] == '/') makedir[strlen(makedir)-1] = 0;
  361.       if(system(makedir) != 0)
  362.     fprintf(stderr, "Error creating directory: %s\n",
  363.         app_resources.userSourceDirectory);
  364.     }
  365.     else
  366.       closedir(dirp);
  367.  
  368.     if((dirp = opendir(app_resources.documentDirectory)) == NULL) {
  369.       if((makedir = s_malloc(strlen(app_resources.documentDirectory) + 12))
  370.      == NULL) {
  371.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  372.     exit(-1);
  373.       }
  374.       strcpy(makedir, "/bin/mkdir ");
  375.       strcat(makedir, app_resources.documentDirectory);
  376.       if(makedir[strlen(makedir)-1] == '/') makedir[strlen(makedir)-1] = 0;
  377.       if(system(makedir) != 0)
  378.     fprintf(stderr, "Error creating directory: %s\n",
  379.         app_resources.documentDirectory);
  380.     }
  381.     else
  382.       closedir(dirp);
  383.   }
  384.   
  385.   {
  386.     char qfilename[256];
  387.     FILE *fp;
  388.  
  389.     if(argc < 1 || argc > 2) {
  390.       fprintf(stderr, "usage: %s [Question_file_name]", argv[0]);
  391.       exit(0);
  392.     }
  393.     else {
  394.  
  395.       the_Question = (XQuestion)s_malloc(sizeof(_XQuestion));
  396.       the_Question->q = (Question)s_malloc(sizeof(_Question));
  397.       the_Question->q->maxresdocs = app_resources.maximumResults;
  398.       the_Question->q->sourcepath = s_malloc((sdir?strlen(sdir):0)+(cdir?strlen(cdir):0)+2);
  399.       sprintf(the_Question->q->sourcepath, "%s:%s", (sdir?sdir:""), (cdir?cdir:""));
  400.       if (argc == 1){ /* we have no quesiton specified.  Let's just make a new one */
  401.     if(app_resources.questionName[0] != 0)
  402.       strcpy(the_Question->q->name, app_resources.questionName);
  403.     else
  404.       strcpy(the_Question->q->name, "New Question");
  405.       }
  406.       else {
  407.     sprintf(qfilename, "%s%s", app_resources.questionDirectory, argv[1]);
  408.     if((fp = fopen(qfilename, "r")) == NULL) {
  409.       fprintf(stderr, "Can't open question %s\n", qfilename);
  410.       exit(-1);
  411.     }
  412.     strcpy(the_Question->q->name, argv[1]);
  413.     ReadQuestion(the_Question->q, fp);
  414.     fclose(fp);
  415.       }
  416.     }
  417.   }
  418.  
  419.   if (app_resources.seedWords[0] != 0
  420.       && the_Question->q->keywords[0] == 0)
  421.     strcpy(the_Question->q->keywords, app_resources.seedWords);
  422.  
  423.   if (app_resources.initialSource[0] != 0
  424.       && the_Question->q->Sources == NULL) {
  425.     the_Question->q->Sources = makeSourceList((SourceID)s_malloc(sizeof(_SourceID)),
  426.                        NULL);
  427.     the_Question->q->Sources->thisSource->filename = s_strdup(app_resources.initialSource);
  428.   }    
  429.  
  430.   CurDpy = XtDisplay(top);
  431.  
  432.   MakeQuestionForm(the_Question);
  433.  
  434.   sourcepopup = (Widget) MakeSourcePopup(top);
  435.  
  436.   messwidget = the_Question->window->StatusWindow;
  437.  
  438.   GetSourceNames(app_resources.userSourceDirectory);
  439.   if(app_resources.commonSourceDirectory[0] != 0)
  440.     GetSourceNames(app_resources.commonSourceDirectory);
  441.  
  442.  
  443.   /* and away we go! */
  444.  
  445.   question = the_Question->q;
  446.  
  447.   sprintf(msg, "X WAIS Question: %s",  question->name);
  448.  
  449.   num_args = 0;
  450.   XtSetArg(arglist[num_args], XtNtitle, msg); num_args++;
  451.   XtSetArg(arglist[num_args], XtNiconName, question->name); num_args++;
  452.   XtSetValues(top, arglist, num_args);
  453.  
  454.   question->modified = TRUE;
  455.  
  456.   sourcewindow = (ScrollList)MakeSourceFrame(top);
  457.   typewindow = (ScrollList)MakeTypeFrame(top);
  458.  
  459.   RebuildListWidget(sourcewindow, Source_items, LIST_NONE);
  460.  
  461.   XtRealizeWidget(top);
  462.  
  463.   /* do all  the necessary stuff */
  464.  
  465.   the_Question->Source_Items =
  466.     buildSourceItemList(question->Sources);
  467.  
  468.   RebuildListWidget(the_Question->window->Sources, the_Question->Source_Items, LIST_NONE);
  469.  
  470.   question->numsources = charlistlength(the_Question->Source_Items);
  471.  
  472.   the_Question->Relevant_Items =
  473.     buildDocumentItemList(question->RelevantDocuments, FALSE);
  474.  
  475.   RebuildListWidget(the_Question->window->RelevantDocuments,
  476.             the_Question->Relevant_Items, LIST_NONE);
  477.  
  478.   question->numdocs = charlistlength(the_Question->Relevant_Items);
  479.  
  480.   the_Question->Result_Items =
  481.     buildDocumentItemList(question->ResultDocuments, TRUE);
  482.  
  483.   RebuildListWidget(the_Question->window->ResultDocuments,
  484.             the_Question->Result_Items, LIST_NONE);
  485.  
  486.   question->numresdocs = charlistlength(the_Question->Result_Items);
  487.  
  488.   if(app_resources.doSearch != False) DoSearch(NULL, NULL, NULL);
  489.  
  490.   rescanint = 1000 * atoi(app_resources.rescanInterval);
  491.  
  492.   rescantimerid = XtAddTimeOut(rescanint, ScanDirs, (Opaque) ScanDirs);
  493.  
  494.   /*freopen("/dev/null", "a", stderr);*/
  495.  
  496.   MakeWaisCursors();
  497.   fuzzButtons(false);
  498.  
  499.   XtSetArg(arglist[0], XtNsensitive, False);
  500.   XtSetValues(viewbutton, arglist, ONE);
  501.   XtSetValues(saveAsButton, arglist, ONE);
  502.  
  503.   XtMainLoop();
  504. }
  505.  
  506.